<?php
class Mgroup_alias extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('serversidedatatables');
	}

	function get_user_group_name()
	{
		$this->db->select('user_group_id, user_group_name');
		$this->db->order_by('user_group_name');
		$sql = $this->db->get('t_mtr_user_group');
		return $sql->result();
	}

	function insert_group_alias()
	{
		$creator = $this->session->userdata('username');
		
		$this->session->set_userdata('alias_name', $this->input->post('alias_name'));

		$data = array(
				'alias_name' => $this->input->post('alias_name'),
				'user_group_id' => $this->input->post('user_group_id'),
				'created_by' => $creator
			);
		
		$query = $this->db->insert('t_mtr_user_group_alias', $data);
		if(isset($query))
		{
			return true;
		}
		
	}

	function get_group_alias_data()
	{
		$sIndexColumn = "alias_id";
        $status=""; 
        $defaultOrder = "0"; 
        $userlogin = $this->session->userdata('username');

        $aColumns = array("alias_id", "alias_name", "user_group_name"); 
        $str1 = array(" , ", ", ' '");
        $str2 = array(" ", " ");
        $inputQuery = "select ".str_replace($str1, $str2, implode(", ", $aColumns))." from t_mtr_user_group_alias 
INNER JOIN t_mtr_user_group on t_mtr_user_group_alias.user_group_id = t_mtr_user_group.user_group_id"; 

        return $this->serversidedatatables->executeServerSideProcess($defaultOrder, $status, $userlogin, $inputQuery, $aColumns, $sIndexColumn); 
	}

	function get_spesific_group_alias_data($alias_id)
	{
		$this->db->select('alias_id, alias_name, user_group_id');
		$query = $this->db->get_where('t_mtr_user_group_alias', array('alias_id' => $alias_id));
		return $query->result();
	}

	function update_group_alias($alias_id, $alias_name, $user_group_id)
	{
		$this->session->set_userdata('alias_name_after', $alias_name);
		$this->session->set_userdata('user_group_id_after', $user_group_id);

		$data = array(
               'alias_name' => $alias_name,
               'user_group_id' => $user_group_id
            );

		$this->db->where('alias_id', $alias_id);
		$query = $this->db->update('t_mtr_user_group_alias', $data);
		if(isset($query))
		{
			return true;
		} 
	}

	function delete($alias_id)
	{
		$query = $this->db->delete('t_mtr_user_group_alias', array('alias_id' => $alias_id));
		if(isset($query))
		{
			return true;
		} 
	}

}